package com.hdu.suixianglu;

import java.util.HashSet;
import java.util.Set;

public class lc202 {
    class Solution {
        public boolean isHappy(int n) {
            Set<Integer> set = new HashSet<>();
            while (n != 1)
            {
                if (set.contains(n)) return false;
                set.add(n);
                n = cal(n);
            }
            return true;
        }

        int cal(int n)
        {
            int sum = 0;
            while (n != 0)
            {
                int temp = n % 10;
                sum += temp * temp;
                n /= 10;
            }
            return sum;
        }
    }
}
